package com.example.warehouseplus.webflux.router;

import com.example.warehouseplus.webflux.handler.AuthenticationHandler;
import com.example.warehouseplus.webflux.handler.ItemHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;

@Configuration
public class ItemRouter {

    @Bean
    public RouterFunction<ServerResponse> route(ItemHandler itemHandler, AuthenticationHandler authHandler) {

        return RouterFunctions
                .route(GET("/all").and(accept(APPLICATION_JSON)), itemHandler::getAllItems)
                .andRoute(GET("/warehouse/{name}").and(accept(APPLICATION_JSON)), itemHandler::getItemByName)
                .andRoute(POST("/warehouse").and(accept(APPLICATION_JSON)), itemHandler::createItem)
                .andRoute(PUT("/warehouse/{name}").and(accept(APPLICATION_JSON)), itemHandler::updateItem)
                .andRoute(DELETE("/warehouse/{name}").and(accept(APPLICATION_JSON)), itemHandler::deleteItem)
                .andRoute(POST("/batch").and(accept(APPLICATION_JSON)), itemHandler::uploadItems)
                .andRoute(GET("/price").and(accept(APPLICATION_JSON)), itemHandler::getItemsByPriceRange)
                .andRoute(POST("/login").and(accept(MediaType.APPLICATION_JSON)), authHandler::login);
    }
}
